﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataMappers
{
    public class ClassDataMappers
    {
        //private Dictionary<Type,IEntityMapper<Object, DomainObject<Object, Object>>> _mappers = new Dictionary<Type,IEntityMapper<Object, DomainObject<Object, Object>>>();

        private Dictionary<Type, object> _mappers = new Dictionary<Type, object>();

	    public void add<K,T>(Type t, IEntityMapper<K,T> mapper) where T : IDomainObject<K, T>
                                                                

        {
            _mappers.Add(t, /*(IEntityMapper<K, DomainObject<K, T>>)*/mapper);
            //_mappers.Add(t, 
	    }
	
	    public IEntityMapper<K,T> get<K,T>(T t) where T : IDomainObject<K,T>
        {
		    return (IEntityMapper<K,T>) _mappers[t.GetType()];
	    }
	
	    //internal ICollection<IEntityMapper<Type,Object>> getAllMappers(){
        internal ICollection<Object> getAllMappers()
        {
            //LinkedList<IEntityMapper<Type, Object>> ret = new LinkedList<IEntityMapper<Type, Object>>();
            LinkedList<Object> ret = new LinkedList<Object>();
            foreach (Object value in _mappers.Values)
            {
  //              ret.AddLast((IEntityMapper<Type, Object>)value);
                ret.AddLast(value);
            }
            return ret;
	    }
	    //internal ICollection<IEntityMapper<Type,Object>> getAllMappersInReverseOrder(){
        internal ICollection<Object> getAllMappersInReverseOrder(){
            //LinkedList<IEntityMapper<Type, Object>> ret = new LinkedList<IEntityMapper<Type, Object>>();
            LinkedList<object> ret = new LinkedList<object>();
            foreach (Object value in _mappers.Values)
            {
            //    ret.AddFirst((IEntityMapper<Type, Object>)value);
                ret.AddFirst(value);
            }
            return ret;
        }

        internal IEntityMapper<T1, T2> Get<T1, T2>(object obj) where T2 : IDomainObject<T1, T2>
        {
            return (IEntityMapper<T1, T2>)_mappers[obj.GetType()];
        }

        internal IEntityMapper<T1, T2> Get<T1, T2>(Type t) where T2 : IDomainObject<T1, T2>
        {
            return (IEntityMapper<T1, T2>)_mappers[t];
        }
    }
}
